var gulp = require('gulp');
var git = require('gulp-git');
var zip = require('gulp-zip');
var merge = require('merge-stream');
var del = require('del');
var vinylPaths = require('vinyl-paths');
var Q = require('q');
var package = require('./package.json');
var destDir = './dist/';

gulp.task("clean",function(){
    return gulp.src(destDir).pipe(vinylPaths(del));

});




gulp.task('electron_prebuilt',['clean'],function(){
   
    return merge(gulp.src("./node_modules/electron-prebuilt/dist/*")
	.pipe(gulp.dest(destDir+"build/"))   
    ,

     
gulp.src("./node_modules/electron-prebuilt/dist/**/*")
	.pipe(gulp.dest(destDir+"build/"))   	 
	,
	 gulp.src("./run.bat").pipe(gulp.dest(destDir+"build/"))
	 ,	gulp.src("./build/**")
    .pipe(gulp.dest(destDir+"build/build"))
	 ,
	 gulp.src("./app/renderer/lib/*") 
    .pipe(gulp.dest(destDir+"build/app/renderer/lib"))
	
	,
	
		gulp.src("./app/renderer/public/*")
    .pipe(gulp.dest(destDir+"build/app/renderer/public"))
	,
	
		gulp.src("./app/renderer/public/**/*") 
    .pipe(gulp.dest(destDir+"build/app/renderer/public"))
	
	,
	
		gulp.src("./app/renderer/styles/*.css") 
    .pipe(gulp.dest(destDir+"build/app/renderer/styles"))
	,
	
		gulp.src("./app/renderer/styles/*.png") 
    .pipe(gulp.dest(destDir+"build/app/renderer/styles"))
	,
	
		gulp.src("./app/renderer/styles/**/*.css") 
    .pipe(gulp.dest(destDir+"build/app/renderer/styles"))
	,
	
		gulp.src("./app/renderer/styles/**/*.png") 
    .pipe(gulp.dest(destDir+"build/app/renderer/styles"))
	);

})
var rev=""
gulp.task('getVersion',['electron_prebuilt'] ,function(){
    var deferred = Q.defer();
	
	git.revParse({args:'--short HEAD'}, function (err, hash) {
  //if (err) ... 
        console.log('current git hash: '+hash);
        rev =hash
		deferred.resolve();
    });
    return deferred.promise;
});


gulp.task('default', ['getVersion'],function(){
console.log("rec=",rev)
return gulp.src([destDir+"build/*",destDir+"build/**/*"])
        .pipe(zip('bilibili-react-electron-'+rev+'.zip'))
        .pipe(gulp.dest(destDir));
});